package cn;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import cn.util.WiselySettings;


@Configuration
@EnableAutoConfiguration
@EnableScheduling
@SpringBootApplication
@EnableConfigurationProperties({WiselySettings.class})
public class testApplication extends WebMvcConfigurerAdapter {
	public static void main(String args[]) {
		SpringApplication.run(testApplication.class);
	}
	@Autowired
	WiselySettings wiselySettings;
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
			registry.addResourceHandler("/upload/movie/**").addResourceLocations("file:"+
					 wiselySettings.getPath());
		super.addResourceHandlers(registry);
	}
	/**
	 * 配置安全拦截器
	 * 
	 * @param registry
	 */
	/*public void addInterceptors(InterceptorRegistry registry) {
		//微信会话
		//registry.addInterceptor(new WxSessionInterceptor()).addPathPatterns("/phone/**");
		registry.addInterceptor(new AuthorizeInterceptor()).excludePathPatterns("/admin/login/**,/");
		// 后台拦劫器
		registry.addInterceptor(new UserSecurityInterceptor())
				.addPathPatterns("/admin/**","/market/**","/courses/**")
				.excludePathPatterns("/admin/login/**");
	}*/
}

/**
 * 
 * @author ideation
 *后台拦劫
 */
class UserSecurityInterceptor implements HandlerInterceptor {

	

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = uri.substring(contextPath.length());
		String userName =  (String) request.getSession().getAttribute("loginUser");
		if (userName == null ) {
			request.getRequestDispatcher("/WEB-INF/jsp/admin/timeout.jsp")
					.forward(request, response);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}



